/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.ldap.ILdapContext;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapEntryNotFoundException;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapMultipleEntriesMatchedException;
/*     */ import com.zimbra.cs.ldap.LdapServerType;
/*     */ import com.zimbra.cs.ldap.LdapTODO.TODOEXCEPTIONMAPPING;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZModificationList;
/*     */ import com.zimbra.cs.ldap.ZSearchControls;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEnumeration;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ZLdapHelper
/*     */   extends LdapHelper
/*     */ {
/*     */   ZLdapHelper(LdapProv ldapProv)
/*     */   {
/*  55 */     super(ldapProv);
/*     */   }
/*     */   
/*     */ 
/*     */   public void searchLdap(ILdapContext ldapContext, SearchLdapOptions searchOptions)
/*     */     throws ServiceException
/*     */   {
/*  62 */     ZLdapContext zlc = LdapClient.toZLdapContext(getProv(), ldapContext);
/*  63 */     zlc.searchPaged(searchOptions);
/*     */   }
/*     */   
/*     */   public void deleteEntry(String dn, LdapUsage ldapUsage) throws ServiceException
/*     */   {
/*  68 */     ZLdapContext zlc = null;
/*     */     try {
/*  70 */       zlc = LdapClient.getContext(LdapServerType.MASTER, ldapUsage);
/*  71 */       zlc.deleteEntry(dn);
/*     */     } finally {
/*  73 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   private ZModificationList getModList(ZLdapContext zlc, String dn, Map<String, ? extends Object> attrs, Entry entry) throws ServiceException
/*     */   {
/*  79 */     ZModificationList modList = zlc.createModificationList();
/*     */     
/*  81 */     AttributeManager attrMgr = AttributeManager.getInst();
/*     */     
/*  83 */     for (Map.Entry<String, ? extends Object> attr : attrs.entrySet()) {
/*  84 */       Object v = attr.getValue();
/*  85 */       String key = (String)attr.getKey();
/*  86 */       boolean doAdd = key.charAt(0) == '+';
/*  87 */       boolean doRemove = key.charAt(0) == '-';
/*     */       
/*  89 */       if ((doAdd) || (doRemove))
/*     */       {
/*  91 */         key = key.substring(1);
/*  92 */         if (attrs.containsKey(key)) {
/*  93 */           throw ServiceException.INVALID_REQUEST("can't mix +attrName/-attrName with attrName", null);
/*     */         }
/*     */       }
/*  96 */       boolean containsBinaryData = attrMgr == null ? false : attrMgr.containsBinaryData(key);
/*  97 */       boolean isBinaryTransfer = attrMgr == null ? false : attrMgr.isBinaryTransfer(key);
/*     */       
/*     */ 
/* 100 */       if ((v instanceof Object[]))
/*     */       {
/*     */ 
/*     */ 
/* 104 */         v = Arrays.asList((Object[])v);
/*     */       }
/*     */       
/* 107 */       if ((v instanceof Collection)) {
/* 108 */         Collection c = (Collection)v;
/* 109 */         if (c.size() == 0)
/*     */         {
/* 111 */           if (entry.getAttr(key, false) != null) {
/* 112 */             modList.removeAttr(key, isBinaryTransfer);
/*     */           }
/*     */         }
/*     */         else {
/* 116 */           String[] sa = new String[c.size()];
/* 117 */           int i = 0;
/* 118 */           for (Object o : c) {
/* 119 */             sa[(i++)] = (o == null ? null : o.toString());
/*     */           }
/*     */           
/*     */ 
/* 123 */           if (doAdd) {
/* 124 */             modList.addAttr(key, sa, entry, containsBinaryData, isBinaryTransfer);
/* 125 */           } else if (doRemove) {
/* 126 */             modList.removeAttr(key, sa, entry, containsBinaryData, isBinaryTransfer);
/*     */           } else
/* 128 */             modList.modifyAttr(key, sa, containsBinaryData, isBinaryTransfer);
/*     */         }
/*     */       } else {
/* 131 */         if ((v instanceof Map)) {
/* 132 */           throw ServiceException.FAILURE("Map is not a supported value type", null);
/*     */         }
/* 134 */         String s = v == null ? null : v.toString();
/* 135 */         if (doAdd) {
/* 136 */           modList.addAttr(key, s, entry, containsBinaryData, isBinaryTransfer);
/*     */         }
/* 138 */         else if (doRemove) {
/* 139 */           modList.removeAttr(key, s, entry, containsBinaryData, isBinaryTransfer);
/*     */         }
/*     */         else {
/* 142 */           modList.modifyAttr(key, s, entry, containsBinaryData, isBinaryTransfer);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 147 */     return modList;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public void modifyAttrs(ZLdapContext zlc, String dn, Map<String, ? extends Object> attrs, Entry entry)
/*     */     throws ServiceException
/*     */   {
/* 164 */     ZModificationList modList = getModList(zlc, dn, attrs, entry);
/*     */     
/* 166 */     if (!modList.isEmpty()) {
/* 167 */       zlc.modifyAttributes(dn, modList);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public void modifyEntry(String dn, Map<String, ? extends Object> attrs, Entry entry, LdapUsage ldapUsage)
/*     */     throws ServiceException
/*     */   {
/* 175 */     ZLdapContext zlc = null;
/*     */     try {
/* 177 */       zlc = LdapClient.getContext(LdapServerType.MASTER, ldapUsage);
/* 178 */       modifyAttrs(zlc, dn, attrs, entry);
/*     */     } finally {
/* 180 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean testAndModifyEntry(ZLdapContext zlc, String dn, ZLdapFilter testFilter, Map<String, ? extends Object> attrs, Entry entry)
/*     */     throws ServiceException
/*     */   {
/* 188 */     ZModificationList modList = getModList(zlc, dn, attrs, entry);
/* 189 */     return zlc.testAndModifyAttributes(dn, modList, testFilter);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   @LdapTODO.TODOEXCEPTIONMAPPING
/*     */   public ZSearchResultEntry searchForEntry(String base, ZLdapFilter filter, ZLdapContext initZlc, boolean useMaster, String[] returnAttrs)
/*     */     throws LdapException.LdapMultipleEntriesMatchedException, ServiceException
/*     */   {
/* 198 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 200 */       if (zlc == null) {
/* 201 */         zlc = LdapClient.getContext(LdapServerType.get(useMaster), LdapUsage.SEARCH);
/*     */       }
/*     */       
/* 204 */       ZSearchControls sc = returnAttrs == null ? ZSearchControls.SEARCH_CTLS_SUBTREE() : ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, 0, returnAttrs);
/*     */       
/*     */ 
/*     */ 
/* 208 */       ZSearchResultEnumeration ne = zlc.searchDir(base, filter, sc);
/* 209 */       if (ne.hasMore()) {
/* 210 */         ZSearchResultEntry sr = ne.next();
/* 211 */         String dups; if (ne.hasMore()) {
/* 212 */           dups = LdapUtil.formatMultipleMatchedEntries(sr, ne);
/* 213 */           throw LdapException.MULTIPLE_ENTRIES_MATCHED(base, filter.toFilterString(), dups);
/*     */         }
/* 215 */         ne.close();
/* 216 */         return sr;
/*     */ 
/*     */ 
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/*     */     finally
/*     */     {
/*     */ 
/*     */ 
/* 229 */       if (initZlc == null)
/* 230 */         LdapClient.closeContext(zlc);
/*     */     }
/* 232 */     return null;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public boolean compare(String dn, String attributeName, String assertionValue, ZLdapContext initZlc, boolean useMaster)
/*     */     throws ServiceException
/*     */   {
/* 249 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 251 */       if (zlc == null) {
/* 252 */         zlc = LdapClient.getContext(LdapServerType.get(useMaster), LdapUsage.COMPARE);
/*     */       }
/*     */       
/* 255 */       return zlc.compare(dn, attributeName, assertionValue);
/*     */     } finally {
/* 257 */       if (initZlc == null) {
/* 258 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public ZAttributes getAttributes(ZLdapContext initZlc, LdapServerType ldapServerType, LdapUsage usage, String dn, String[] returnAttrs)
/*     */     throws LdapException.LdapEntryNotFoundException, ServiceException
/*     */   {
/* 267 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 269 */       if (zlc == null) {
/* 270 */         assert (ldapServerType != null);
/* 271 */         assert (usage != null);
/* 272 */         zlc = LdapClient.getContext(ldapServerType, usage);
/*     */       }
/* 274 */       return zlc.getAttributes(dn, returnAttrs);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/*     */     finally
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 286 */       if (initZlc == null) {
/* 287 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public ZSearchResultEnumeration searchDir(String baseDN, ZLdapFilter filter, ZSearchControls searchControls, ZLdapContext initZlc, LdapServerType ldapServerType)
/*     */     throws ServiceException
/*     */   {
/* 296 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 298 */       if (zlc == null) {
/* 299 */         zlc = LdapClient.getContext(ldapServerType, LdapUsage.SEARCH);
/*     */       }
/* 301 */       return zlc.searchDir(baseDN, filter, searchControls);
/*     */ 
/*     */ 
/*     */     }
/*     */     finally
/*     */     {
/*     */ 
/*     */ 
/* 309 */       if (initZlc == null) {
/* 310 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   protected long countEntriesByNoopSearch(String baseDN, ZLdapFilter filter, ZSearchControls searchControls, ZLdapContext initZlc, LdapServerType ldapServerType)
/*     */     throws ServiceException
/*     */   {
/* 319 */     ZLdapContext zlc = initZlc;
/*     */     try {
/* 321 */       if (zlc == null) {
/* 322 */         zlc = LdapClient.getContext(ldapServerType, LdapUsage.SEARCH);
/*     */       }
/* 324 */       return zlc.countEntries(baseDN, filter, searchControls);
/*     */     } finally {
/* 326 */       if (initZlc == null) {
/* 327 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/ZLdapHelper.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */